package com.sdhs.paas.flux.app;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;

import com.sdhs.paas.flux.model.User;

import ch.qos.logback.core.net.SyslogOutputStream;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/user")
public class UserController {
//    @Autowired
//    private UserService userService;

    @PostMapping("")
    public Mono<User> save() {
    	User user = new User();
        System.err.println("findOneCity 1");
        Mono mono = Mono.create(cityMonoSink -> cityMonoSink.success(user));
        System.err.println("findOneCity 3");
        return mono;
    }

    @DeleteMapping("/{username}")
    public Mono<Long> deleteByUsername(@PathVariable String username) {
    	System.out.println(username);
    	Mono mono = Mono.create(user -> user.success(1));
    	System.out.println("game over");
    	return mono;
    }

    @GetMapping("/{username}")
    public Mono<User> findByUsername(@PathVariable String username) {
    	System.out.println(username);
    	Mono mono = Mono.create(user -> user.success(new User()));
    	System.out.println("game over");
    	return mono;
    }

    @GetMapping(value = "", produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
    public Flux<User> findAll() {
    	List<User> userList = new ArrayList<User>();
    	User user = null;
    	for(int i=0 ;i < 2;i ++){
    		user = new User();
    		user.setId("id-"+i);
    		user.setName("name"+i);
    		user.setPhone("1526918541"+i);
    		user.setUsername("username"+i);
    		userList.add(user);
    	}
        System.out.println("findAllCity 1");
        Flux flux = Flux.create(userFluxSink -> {
        	userList.forEach(u -> userFluxSink.next(u));
        	userFluxSink.complete();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("findAllCity 2");
        });
        System.out.println("findAllCity  end 3");
        return flux;
    }
    
    
}
